package com.webinsky.article.persist.dao;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

import com.webinsky.article.domain.MisArticleFilter;
import com.webinsky.article.persist.entity.Article;
import com.webinsky.article.persist.entity.ArticleUser;

public interface ArticleMapper {
	 @Insert({
		"insert into article (site_id, file_name, article_no, title, abstract, remark, user_id, can_modify, update_user_id, update_user_name, del, create_at, update_at, language)",
		"values (#{siteId, jdbcType=INTEGER}, #{fileName,jdbcType=VARCHAR}, #{articleNo,jdbcType=VARCHAR}, #{title,jdbcType=VARCHAR}, #{abs,jdbcType=VARCHAR},  #{remark, jdbcType=VARCHAR}, #{userId,jdbcType=INTEGER}, #{canModify,jdbcType=INTEGER}, #{updateUserId,jdbcType=INTEGER}, #{updateUserName, jdbcType=VARCHAR}, #{del,jdbcType=INTEGER},",
		"now(), now(), #{language,jdbcType=VARCHAR})"
	})
	@SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Integer.class)
	int insert(Article record);
	    
	@Select({
	    "select",
	    "id, site_id, file_name, article_no, title, abstract, remark, del, user_id, can_modify, update_user_id, update_user_name, create_at, update_at, language",
	    "from article"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
	    @Result(column="file_name", property="fileName", jdbcType=JdbcType.VARCHAR),
	    @Result(column="article_no", property="articleNo", jdbcType=JdbcType.VARCHAR),
	    @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
	    @Result(column="abstract", property="abs", jdbcType=JdbcType.VARCHAR),
	    @Result(column="remark", property="remark", jdbcType=JdbcType.VARCHAR),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
	    @Result(column="can_modify", property="canModify", jdbcType=JdbcType.INTEGER),
	    @Result(column="update_user_id", property="updateUserId", jdbcType=JdbcType.INTEGER),
	    @Result(column="update_user_name", property="updateUserName", jdbcType=JdbcType.VARCHAR),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR)
	})
	List<Article> selectAll();
	
	@Select({
	    "select",
	    "id, site_id, file_name, article_no, title, abstract, remark, del, user_id, can_modify, update_user_id, update_user_name, create_at, update_at, language",
	    "from article",
	    "where del = 0 and site_id = #{siteId, jdbcType=INTEGER}"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
	    @Result(column="file_name", property="fileName", jdbcType=JdbcType.VARCHAR),
	    @Result(column="article_no", property="articleNo", jdbcType=JdbcType.VARCHAR),
	    @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
	    @Result(column="abstract", property="abs", jdbcType=JdbcType.VARCHAR),
	    @Result(column="remark", property="remark", jdbcType=JdbcType.VARCHAR),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
	    @Result(column="can_modify", property="canModify", jdbcType=JdbcType.INTEGER),
	    @Result(column="update_user_id", property="updateUserId", jdbcType=JdbcType.INTEGER),
	    @Result(column="update_user_name", property="updateUserName", jdbcType=JdbcType.VARCHAR),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR)
	})
	List<Article> selectActiveBySiteId(@Param("siteId")int siteId);
	
	@Select({
	    "select",
	    "id, site_id, file_name, article_no, title, abstract, remark, del, user_id, can_modify, update_user_id, update_user_name, create_at, update_at, language",
	    "from article",
	    "where del = 0 and site_id = #{siteId, jdbcType=INTEGER} and language = #{lang, jdbcType=VARCHAR}"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
	    @Result(column="file_name", property="fileName", jdbcType=JdbcType.VARCHAR),
	    @Result(column="article_no", property="articleNo", jdbcType=JdbcType.VARCHAR),
	    @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
	    @Result(column="abstract", property="abs", jdbcType=JdbcType.VARCHAR),
	    @Result(column="remark", property="remark", jdbcType=JdbcType.VARCHAR),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
	    @Result(column="can_modify", property="canModify", jdbcType=JdbcType.INTEGER),
	    @Result(column="update_user_id", property="updateUserId", jdbcType=JdbcType.INTEGER),
	    @Result(column="update_user_name", property="updateUserName", jdbcType=JdbcType.VARCHAR),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR)
	})
	List<Article> selectActiveBySiteIdAndLang(@Param("siteId")int siteId, @Param("lang")String lang);	
	
	@SelectProvider(type = ArticleMapperProvider.class, method = "selectByIds")
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
	    @Result(column="file_name", property="fileName", jdbcType=JdbcType.VARCHAR),
	    @Result(column="article_no", property="articleNo", jdbcType=JdbcType.VARCHAR),
	    @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
	    @Result(column="abstract", property="abs", jdbcType=JdbcType.VARCHAR),
	    @Result(column="remark", property="remark", jdbcType=JdbcType.VARCHAR),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
	    @Result(column="can_modify", property="canModify", jdbcType=JdbcType.INTEGER),
	    @Result(column="update_user_id", property="updateUserId", jdbcType=JdbcType.INTEGER),
	    @Result(column="update_user_name", property="updateUserName", jdbcType=JdbcType.VARCHAR),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR)
	})
	List<Article> selectByIds(@Param("list")List<Integer> ids);
	
	@SelectProvider(type = ArticleMapperProvider.class, method = "selectByNos")
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
	    @Result(column="file_name", property="fileName", jdbcType=JdbcType.VARCHAR),
	    @Result(column="article_no", property="articleNo", jdbcType=JdbcType.VARCHAR),
	    @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
	    @Result(column="abstract", property="abs", jdbcType=JdbcType.VARCHAR),
	    @Result(column="remark", property="remark", jdbcType=JdbcType.VARCHAR),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
	    @Result(column="can_modify", property="canModify", jdbcType=JdbcType.INTEGER),
	    @Result(column="update_user_id", property="updateUserId", jdbcType=JdbcType.INTEGER),
	    @Result(column="update_user_name", property="updateUserName", jdbcType=JdbcType.VARCHAR),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR)
	})
	List<Article> selectByNos(@Param("articleNos")List<String> nos, @Param("siteId")Integer siteId, @Param("lang")String lang);
	
	@Select({
	    "select",
	    "id, site_id, file_name, article_no, title, abstract, remark, del, user_id, can_modify, update_user_id, update_user_name, create_at, update_at, language",
	    "from article ",
	    "where id = #{id, jdbcType=INTEGER}"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
	    @Result(column="file_name", property="fileName", jdbcType=JdbcType.VARCHAR),
	    @Result(column="article_no", property="articleNo", jdbcType=JdbcType.VARCHAR),
	    @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
	    @Result(column="abstract", property="abs", jdbcType=JdbcType.VARCHAR),
	    @Result(column="remark", property="remark", jdbcType=JdbcType.VARCHAR),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
	    @Result(column="can_modify", property="canModify", jdbcType=JdbcType.INTEGER),
	    @Result(column="update_user_id", property="updateUserId", jdbcType=JdbcType.INTEGER),
	    @Result(column="update_user_name", property="updateUserName", jdbcType=JdbcType.VARCHAR),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR)
	})	
	Article selectById(@Param("id")int id);
	
	@SelectProvider(type = ArticleMapperProvider.class, method = "selectByFilter")
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="article_no", property="articleNo", jdbcType=JdbcType.VARCHAR),
	    @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
	    @Result(column="file_name", property="fileName", jdbcType=JdbcType.VARCHAR),
	    @Result(column="abstract", property="summary", jdbcType=JdbcType.VARCHAR),
	    @Result(column="remark", property="remark", jdbcType=JdbcType.VARCHAR),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
	    @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
	    @Result(column="can_modify", property="canModify", jdbcType=JdbcType.INTEGER),
	    @Result(column="update_user_id", property="updateUserId", jdbcType=JdbcType.INTEGER),
	    @Result(column="update_user_name", property="updateUserName", jdbcType=JdbcType.VARCHAR),
	    @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	})
	List<ArticleUser> selectArticleUserByFilter(@Param("filter")MisArticleFilter filter);
	
	@SelectProvider(type = ArticleMapperProvider.class, method = "selectCountByFilter")
	int selectArticleUserCountByFilter(@Param("filter")MisArticleFilter filter);
	
	@Select({
	    "select",
	    "count(*)",
	    "from article ",
	    "where  language = #{lang, jdbcType=VARCHAR} and site_id = #{siteId, jdbcType=INTEGER} and (article_no = #{no, jdbcType=VARCHAR} or title = #{title, jdbcType=VARCHAR} or file_name = #{fileName, jdbcType=VARCHAR})"
	})
	int selectCountByName(@Param("no")String no, @Param("title")String title, @Param("fileName")String fileName, @Param("lang")String lang, @Param("siteId")int siteId);

	@Select({
	    "select",
	    "count(*)",
	    "from article ",
	    "where  language = #{lang, jdbcType=VARCHAR} and site_id = #{siteId, jdbcType=INTEGER} and del = 0"
	})
	int selectBySiteIdAndLang(@Param("siteId")int siteId, @Param("lang")String lang);
	
	@Update({
        "update article",
	        "set file_name = #{fileName,jdbcType=VARCHAR},",
	        "site_id = #{siteId,jdbcType=INTEGER},",
	        "article_no = #{articleNo,jdbcType=VARCHAR},",
	        "title = #{title,jdbcType=VARCHAR},",
	        "abstract = #{abs,jdbcType=VARCHAR},",
	        "remark = #{remark,jdbcType=VARCHAR},",
	        "del = #{del, jdbcType=INTEGER},",
	        "can_modify = #{canModify, jdbcType=INTEGER},",
	        "create_at = #{createAt,jdbcType=TIMESTAMP},",
	        "update_at = #{updateAt,jdbcType=TIMESTAMP},",
	        "update_user_id = #{updateUserId, jdbcType=INTEGER},",
	        "update_user_name = #{updateUserName, jdbcType=VARCHAR},",
	        "language = #{language,jdbcType=VARCHAR}",
        "where Id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Article record);
}
